package com.project.service.sys;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.project.dao.sys.AreaDao;
import com.project.entity.sys.Area;
import com.project.service.BaseService;

@Service
public class AreaService extends BaseService<Area>{
	
	private static final Logger logger = LoggerFactory.getLogger(AreaService.class);
	
	@Autowired
	private AreaDao areaDao;
	
	@Cacheable(value="provinceListCache")
	public List<Area> getProvinceList(){
		logger.info("------------获取省级数据--------------");
		return areaDao.getProvinceList();
	}
	
	@Cacheable(value="childListCache")
	public List<Area> getChildList(String code){
		logger.info("------------获取市级数据code:{}--------------",code);
		return areaDao.getChildList(code);
	}
	
	public String getAreaNameByCode(String code){
		return areaDao.getAreaNameByCode(code);
	}
	
	public Map<String,Area> getProCityMun(){
		HashMap<String, Area> pMap = new HashMap<String, Area>();
		
		List<Area> pList = getProvinceList();
		for (Area province : pList) {
			List<Area> cList = areaDao.getChildList(province.getCode());
			province.setAreaList(cList);
			
			HashMap<String, Area> cMap = new HashMap<String, Area>();
			for (Area city : cList) {
				List<Area> mList = areaDao.getChildList(city.getCode());
				city.setAreaList(mList);

				HashMap<String, Area> mMap = new HashMap<String, Area>();
				for (Area municipality: mList) {
					mMap.put(municipality.getCode(), municipality);
				}
				city.setAityMap(mMap);
				cMap.put(city.getCode(), city);
			}
			province.setAityMap(cMap);
			pMap.put(province.getCode(), province);
		}
		return pMap;
	}

}
